package com.wc.codeforces.DP.线性DP.Rudolf_and_the_Ball_Game;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/6/23 17:27
 * @description https://codeforces.com/problemset/problem/1941/D
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010;
    // f[i][j] 表示第i次投完, 落到了j手上
    static boolean[][] f = new boolean[N][N];
    static int n, m, x;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            m = sc.nextInt();
            n = sc.nextInt();
            x = sc.nextInt() - 1;
            for (int i = 0; i <= n; i++) Arrays.fill(f[i], 0, m + 1, false);
            f[0][x] = true;
            for (int i = 1; i <= n; i++) {
                x = sc.nextInt();
                char c = sc.next().charAt(0);
                if (c == '0') dp1(i);
                else if (c == '1') dp2(i);
                else {
                    dp1(i);
                    dp2(i);
                }
            }
            int num = 0;
            for (int i = 0; i < m; i++) {
                if (f[n][i]) num++;
            }
            out.println(num);
            for (int i = 0; i < m; i++) {
                if (f[n][i]) out.print((i + 1) + " ");
            }
            out.println();
        }
        out.flush();
    }

    // 顺时针
    static void dp1(int i) {
        for (int j = 0; j < m; j++) {
            int k = (j - x + m) % m;
            f[i][j] |= f[i - 1][k];
        }
    }

    // 逆时针
    static void dp2(int i) {
        for (int j = 0; j < m; j++) {
            int k = (j + x) % m;
            f[i][j] |= f[i - 1][k];
        }
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
